package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import com.itheima.utils.TliasException;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 员工管理
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;
//分页查询,key:pageHelper.startPage,empMapper的返回值为list<Emp>,
//    @Override
//    public PageResult findPage(EmpQueryParam empQueryParam) {
//        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
//        Page<Emp> page1 = (Page<Emp>) empMapper.list(empQueryParam);
////        Long total = empMapper.count();
////        int stPage = (page -1)*pageSize;
////        List<Emp> empList = empMapper.findPage(stPage, pageSize);
//        Long total = page1.getTotal();
//        List<Emp> empList = page1.getResult();
//        return new PageResult(total, empList);
//    }
////两个表的保存 key:副表(!CollectionsUntil.isempty()判断后放入循环,给经验的员工id赋值
//
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public void save(Emp emp) {
//        emp.setCreateTime(LocalDateTime.now());
//        emp.setUpdateTime(LocalDateTime.now());
//        emp.setPassword("123456");
//        empMapper.savelist(emp);
//        //日记事物 @Transactional(propagation = Propagation.REQUIRES_NEW)
//        EmpLog empLog = new EmpLog(null, LocalDateTime.now(), emp.toString());
//        empLogService.insertLog(empLog);
//
//
//        List<EmpExpr> exprList = emp.getExprList();
//        int empId = emp.getId();
//        if (!CollectionUtils.isEmpty(exprList)) {
//            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
//            empExprMapper.saveExprList(exprList);
//        }
//
//    }
////
////两个表删除
//    @Override
//    public void delete(List<Integer> ids) {
//        empMapper.deleteById(ids);
//
//        empExprMapper.deleteByEmpid(ids);
//    }
////两个表的查询
//    @Override
//    public Emp getById(Integer id) {
//        Emp emp = empMapper.getById(id);
//        List<EmpExpr> exprList = empExprMapper.getByEmpid(id);
//        emp.setExprList(exprList);
//        return emp;
//    }
//// 两个表的更新: 副表先删后加,empExprMapper的del和save方法搬过来,
//// Arrays.asList把empId转换为集合再放入删除方法
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void updateById(Emp emp) {
//        emp.setUpdateTime(LocalDateTime.now());
//        empMapper.updateById(emp);
//
//        empExprMapper.deleteByEmpid(Arrays.asList(emp.getId()));
//
//        int empId = emp.getId();
//        List<EmpExpr> exprList = emp.getExprList();
//        if (!CollectionUtils.isEmpty(exprList)) {
//            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
//            empExprMapper.saveExprList(exprList);
//        }
//
//    }
//统计收集工作 看文档,要集合,自定义pojo类,放入集合,mapper返回的map集合(List<Map>)用
// empMapper来收集,再循环中两个集合add(map.get("键名 "))赋值,最后返回pojo类名(集合名1,集合名2)
//不要集合:直接map集合收集empMapper,结果是成对的一个个集合
    @Override
    public JobOption getEmpJobData() {
        List<Map<String, Object>> list = empMapper.countEmpJobData();
        List jobList = new ArrayList<>();
        List dataList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            jobList.add(map.get("jobName"));
            dataList.add(map.get("total"));
        }
        return new JobOption(jobList, dataList);
    }
//统计收集性别
    @Override
    public List<Map> getgender() {
        return empMapper.getgender();
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveEmpAndExpr(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        emp.setPassword("123456");
        this.save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach((expr)->{
            expr.setEmpId(emp.getId());
        });
        empExprMapper.insert(exprList);
    }
@Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteEmpAndExpr(List<Integer> ids) {
        this.removeByIds(ids);
        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(EmpExpr::getEmpId,ids);
        empExprMapper.delete(wrapper);

    }

    @Override
    public Emp getEmpAndExprById(Integer id) {
        Emp emp = this.getById(id);
        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmpExpr::getEmpId,id);
        List<EmpExpr> exprList = empExprMapper.selectList(wrapper);
        emp.setExprList(exprList);
        return emp;

    }

    @Override
    public void updateEmpAndExprById(Emp emp) {
        this.updateById(emp);
        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmpExpr::getEmpId,emp.getId());
        empExprMapper.delete(wrapper);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach((expr -> {
            expr.setEmpId(emp.getId());
        }));


        empExprMapper.insert(exprList);

    }

    @Override
    public LoginInfo findLoginInfo(EmpDto empDto) {
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Emp::getUsername, empDto.getUsername());
        queryWrapper.eq(Emp::getPassword, empDto.getPassword());
        Emp emp = this.getOne(queryWrapper);
        if (emp == null){
            throw new TliasException("用户名或密码错误");
        }
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setId(emp.getId());
        loginInfo.setName(emp.getName());
        loginInfo.setUsername(emp.getUsername());

        // 生成令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", emp.getId());
        claims.put("name", emp.getName());
        String token = JwtUtils.generateJwt(claims);
        loginInfo.setToken(token);
        return loginInfo;

    }

//    @Override
//    public List<Emp> findAll() {
//        List<Emp> empList =empMapper.findAll();
//        return empList;
//    }

    //统计收集工作 看文档,要集合,自定义pojo类,放入集合,mapper返回的map集合(List<Map>)用
// empMapper来收集,两个集合add(map.get("键名 "))
//不要集合:直接map集合收集empMapper,结果是成对的一个个集合
//    public JobOption collectionInfo(){
//        List<Map<String, Object>> maps = empMapper.countEmpJobData();
//         List onelist = new ArrayList<>();
//         List onetwo = new ArrayList<>();
//        for (Map<String, Object> map : maps) {
//            onelist.add(map.get("jobName"));
//            onetwo.add(map.get("total"));
//        }
//
//         return new JobOption(onelist,onetwo);
//    }

//    public void update(Emp emp){
//        empMapper.updateById(emp);
//        int empId =emp.getId();
//
//        empExprMapper.deleteByEmpid(Arrays.asList(empId));
//
//        List<EmpExpr> exprList = emp.getExprList();
//        if (!CollectionUtils.isEmpty(exprList)) {
//            for (EmpExpr expr : exprList) {
//                expr.setEmpId(empId);
//
//            }
//            empExprMapper.saveExprList(exprList);
//        }
//
//
//    }


}